﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;
using System.Data;


namespace CRM.Web.UserManage
{
    public partial class List : Common.PageBase
    {

        public string ModifyUrl = "Modify.aspx?USERID={0}";
        T_USER_BLL bll = new T_USER_BLL();
        //SqlServerDAL<T_USER_Entity> dal = new SqlServerDAL<T_USER_Entity>();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);


            gvList.PageSize = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "PageSize"), 20);
            PageHelper.SetPopCalender(this.Page, txtLASTLOGINTIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtLASTLOGINTIMEEnd);
            PageHelper.SetPopCalender(this.Page, txtBIRTHDATEBegin);
            PageHelper.SetPopCalender(this.Page, txtBIRTHDATEEnd);
            PageHelper.SetPopCalender(this.Page, txtPWDLASTMODIFYTIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtPWDLASTMODIFYTIMEEnd);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEEnd);
            PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIMEEnd);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblExportMaxRows.Text = configBll.GetCacheValueByName(this.Page, "ExportMaxRows");
                txtRecordBegin.Text = StaticConstant.RecordBegin;
                txtRecordEnd.Text = StaticConstant.RecordEnd;

                ddlDEPARTMENTIDBinding();
                ddlROLEIDBinding();
                ddlSEATIDBinding();
                ddlCREATEPEOPLEIDBinding();
                ddlLASTMODIFYPEOPLEIDBinding();
                ddlSTATUSBinding();

                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定


        private void ddlSTATUSBinding()
        {
            ddlSTATUS.Items.Clear();
            EnumHelper.FillListControl(ddlSTATUS, typeof(Status));
            ddlSTATUS.Items.Insert(0, new ListItem("", ""));
        }


        /// <summary>
        ///	部门编号 下拉列表数据绑定
        /// </summary>
        private void ddlDEPARTMENTIDBinding()
        {
            ddlDEPARTMENTID.DataSource = null;
            ddlDEPARTMENTID.DataTextField = "";
            ddlDEPARTMENTID.DataValueField = "";
            ddlDEPARTMENTID.DataBind();
            ddlDEPARTMENTID.Items.Insert(0, new ListItem("", ""));
        }



        /// <summary>
        ///	角色编号 下拉列表数据绑定
        /// </summary>
        private void ddlROLEIDBinding()
        {
            ddlROLEID.DataSource = null;
            ddlROLEID.DataTextField = "";
            ddlROLEID.DataValueField = "";
            ddlROLEID.DataBind();
            ddlROLEID.Items.Insert(0, new ListItem("", ""));
        }







        /// <summary>
        ///	座席编号 下拉列表数据绑定
        /// </summary>
        private void ddlSEATIDBinding()
        {
            ddlSEATID.DataSource = null;
            ddlSEATID.DataTextField = "";
            ddlSEATID.DataValueField = "";
            ddlSEATID.DataBind();
            ddlSEATID.Items.Insert(0, new ListItem("", ""));
        }













        /// <summary>
        ///	创建人编号 下拉列表数据绑定
        /// </summary>
        private void ddlCREATEPEOPLEIDBinding()
        {
            ddlCREATEPEOPLEID.DataSource = null;
            ddlCREATEPEOPLEID.DataTextField = "";
            ddlCREATEPEOPLEID.DataValueField = "";
            ddlCREATEPEOPLEID.DataBind();
            ddlCREATEPEOPLEID.Items.Insert(0, new ListItem("", ""));
        }



        /// <summary>
        ///	最后修改人编号 下拉列表数据绑定
        /// </summary>
        private void ddlLASTMODIFYPEOPLEIDBinding()
        {
            ddlLASTMODIFYPEOPLEID.DataSource = null;
            ddlLASTMODIFYPEOPLEID.DataTextField = "";
            ddlLASTMODIFYPEOPLEID.DataValueField = "";
            ddlLASTMODIFYPEOPLEID.DataBind();
            ddlLASTMODIFYPEOPLEID.Items.Insert(0, new ListItem("", ""));
        }






        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["USERID"]))
            {
                sql.Append(" and USERID like '%" + Request.QueryString["USERID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["USERNAME"]))
            {
                sql.Append(" and USERNAME like '%" + Request.QueryString["USERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LOGINNAME"]))
            {
                sql.Append(" and LOGINNAME like '%" + Request.QueryString["LOGINNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PASSWORD"]))
            {
                sql.Append(" and PASSWORD like '%" + Request.QueryString["PASSWORD"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GENDER"]))
            {
                sql.Append(" and GENDER like '%" + Request.QueryString["GENDER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEPARTMENTID"]))
            {
                sql.Append(" and DEPARTMENTID = '" + Request.QueryString["DEPARTMENTID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEPARTMENTNAME"]))
            {
                sql.Append(" and DEPARTMENTNAME like '%" + Request.QueryString["DEPARTMENTNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ROLEID"]))
            {
                sql.Append(" and ROLEID = '" + Request.QueryString["ROLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ROLENAME"]))
            {
                sql.Append(" and ROLENAME like '%" + Request.QueryString["ROLENAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LOGINCOUNT"]))
            {
                sql.Append(" and LOGINCOUNT like '%" + Request.QueryString["LOGINCOUNT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTLOGINTIMEBegin"]))
            {
                sql.Append(String.Format(" and {0} ", bll._dal.GetBeginDateString("LASTLOGINTIME", Request.QueryString["LASTLOGINTIMEBegin"].Trim())));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTLOGINTIMEEnd"]))
            {
                sql.Append(String.Format(" and {0} ", bll._dal.GetEndDateString("LASTLOGINTIME", Request.QueryString["LASTLOGINTIMEEnd"].Trim())));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUS"]))
            {
                sql.Append(" and STATUS like '%" + Request.QueryString["STATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIRTHDATEBegin"]))
            {
                sql.Append(String.Format(" and {0} ", bll._dal.GetBeginDateString("BIRTHDATE", Request.QueryString["BIRTHDATEBegin"].Trim())));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIRTHDATEEnd"]))
            {
                sql.Append(String.Format(" and {0} ", bll._dal.GetEndDateString("BIRTHDATE", Request.QueryString["BIRTHDATEEnd"].Trim())));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SEATID"]))
            {
                sql.Append(" and SEATID = '" + Request.QueryString["SEATID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PWDLASTMODIFYTIMEBegin"]))
            {
                sql.Append(String.Format(" and {0} ", bll._dal.GetBeginDateString("PWDLASTMODIFYTIME", Request.QueryString["PWDLASTMODIFYTIMEBegin"].Trim())));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PWDLASTMODIFYTIMEEnd"]))
            {
                sql.Append(String.Format(" and {0} ", bll._dal.GetEndDateString("PWDLASTMODIFYTIME", Request.QueryString["PWDLASTMODIFYTIMEEnd"].Trim())));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LOCKED"]))
            {
                sql.Append(" and LOCKED like '%" + Request.QueryString["LOCKED"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PWDRESET"]))
            {
                sql.Append(" and PWDRESET like '%" + Request.QueryString["PWDRESET"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MOBILEPHONE"]))
            {
                sql.Append(" and MOBILEPHONE like '%" + Request.QueryString["MOBILEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["HOMEPHONE"]))
            {
                sql.Append(" and HOMEPHONE like '%" + Request.QueryString["HOMEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EMAIL"]))
            {
                sql.Append(" and EMAIL like '%" + Request.QueryString["EMAIL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["URL"]))
            {
                sql.Append(" and URL like '%" + Request.QueryString["URL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["QQ"]))
            {
                sql.Append(" and QQ like '%" + Request.QueryString["QQ"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MSN"]))
            {
                sql.Append(" and MSN like '%" + Request.QueryString["MSN"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EXT"]))
            {
                sql.Append(" and EXT like '%" + Request.QueryString["EXT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(String.Format(" and {0} ", bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim())));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(String.Format(" and {0} ", bll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim())));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID = '" + Request.QueryString["CREATEPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID = '" + Request.QueryString["LASTMODIFYPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                sql.Append(String.Format(" and {0} ", bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"].Trim())));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                sql.Append(String.Format(" and {0} ", bll._dal.GetEndDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"].Trim())));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            sql.Append(" and (ISSYSTEMADMIN <> '1' or ISSYSTEMADMIN is null) ");
            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }
            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_USER_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "USERID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }


        protected void btnExportExcel_Click(object sender, EventArgs e)
        {

            string fileName = "user";
            fileName += _userLoginInfo.UserEntity.LOGINNAME.ToUpper();
            if (UserLoginInfo.IsAdministrator())
            {
                PageHelper.ShowMessage(this.Page, "请不要用系统管理导出数据！");
                return;
            }

            int recordBegin = StringHelper.ToInt(txtRecordBegin.Text.Trim(), 0);
            if (recordBegin != 0)
            {
                recordBegin--;
            }
            int recordEnd = StringHelper.ToInt(txtRecordEnd.Text.Trim(), 0);
            if (recordEnd != 0)
            {
                recordEnd--;
            }
            int recordCount = recordEnd - recordBegin;
            if (recordCount >= StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "ExportMaxRows"), 10000))
            {
                PageHelper.ShowMessage("导出区间大于单次导出数！");
                return;
            }

            StringBuilder sql = GenerationSQLString();
            DataTable dataTable = bll.GetDataTable(sql.ToString(), recordBegin, recordCount, gvList.SortConditions);


            Dictionary<string, string> userType = new Dictionary<string, string>();
            Dictionary<string, string> gender = new Dictionary<string, string>();
            Dictionary<string, string> status = new Dictionary<string, string>();
            Dictionary<string, string> locked = new Dictionary<string, string>();


            try
            {
                userType = EnumHelper.EnumToDictionary(typeof(UserType)).ToDictionary(p => p.Key.ToString(), p => p.Value);

            }
            catch (Exception ex)
            {

            }

            try
            {
                gender = EnumHelper.EnumToDictionary(typeof(Gender)).ToDictionary(p => p.Key.ToString(), p => p.Value);

            }
            catch (Exception ex)
            {

            }

            try
            {
                status = EnumHelper.EnumToDictionary(typeof(Status)).ToDictionary(p => p.Key.ToString(), p => p.Value);

            }
            catch (Exception ex)
            {

            }

            try
            {
                locked = EnumHelper.EnumToDictionary(typeof(IsLocked)).ToDictionary(p => p.Key.ToString(), p => p.Value);

            }
            catch (Exception ex)
            {

            }
           

            List<NPOIExcelHelper.FieldInfo> fieldInfos = new List<NPOIExcelHelper.FieldInfo>();

            #region 导出数据列
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("USERID", "用户编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("USERNAME", "用户姓名"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LOGINNAME", "登录名"));
            //fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PASSWORD", "密码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("GENDER", "性别", gender));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("DEPARTMENTID", "部门编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("DEPARTMENTCODE", "部门代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("DEPARTMENTNAME", "部门名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ROLEID", "角色编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ROLENAME", "角色名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LOGINCOUNT", "登录次数"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTLOGINTIME", "最后登录时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTLOGINIP", "最后登录IP"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("STATUS", "状态", status));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BIRTHDATE", "生日"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SEATID", "座席编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PWDLASTMODIFYTIME", "密码最后修改时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LOCKED", "是否已锁", locked));
            //fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PWDRESET", "密码是否重置"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MOBILEPHONE", "手机"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("HOMEPHONE", "家庭电话"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("EMAIL", "邮件地址"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("URL", "网址"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("QQ", "QQ"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MSN", "MSN"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("EXT", "分机号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATETIME", "创建时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEPEOPLEID", "创建人编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEPEOPLE", "创建人"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYPEOPLEID", "最后修改人编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYPEOPLE", "最后修改人"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYTIME", "最后修改时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("REMARK", "备注"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ISSYSTEMADMIN", "系统管理员"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONID", "事业部编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONNAME", "事业部名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONCODE", "事业部代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("USERTYPE", "用户类型", userType));

            #endregion

            NPOIExcelHelper.MemoryStreamMaxRows = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "MemoryStreamMaxRows"), 0);
            fileName = NPOIExcelHelper.DataTableToExcel(dataTable, fieldInfos, fileName + ".xls");
            UserLoginInfo.WriteLog(LogType.导出, "用户导出", "用户管理", fileName);
            dataTable.Clear();
            dataTable = null;
            GC.Collect();
        }


        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_USER_Entity entity = new T_USER_Entity();
            entity.USERID = StringHelper.ToLong((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());

            long rel = bll.Delete(entity);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {                                        
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "LOCKED":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = ((IsLocked)Enum.Parse(typeof(IsLocked), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {
                                                
                                            }
                                        }
                                        break;
                                    case "GENDER":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = ((Gender)Enum.Parse(typeof(Gender), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {
                                                
                                            }
                                        }
                                        break;
                                    case "STATUS":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                if (temp == ((int)Status.启用).ToString())
                                                {
                                                    LinkButton lbl = e.Row.FindControl("lblEnable") as LinkButton;
                                                    if (lbl != null)
                                                    {
                                                        lbl.Visible = false;
                                                    }
                                                }
                                                else
                                                {
                                                    LinkButton lbl = e.Row.FindControl("lblDisable") as LinkButton;
                                                    if (lbl != null)
                                                    {
                                                        lbl.Visible = false;
                                                    }
                                                }
                                                temp = ((Status)Enum.Parse(typeof(Status), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {
                                                
                                            }
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// Handles the RowCommand event of the gvList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "ResetPWD":
                    {
                        string userId = e.CommandArgument.ToString();
                        if (ResetPassword(userId)) return;
                        Response.Redirect(HttpContext.Current.Request.RawUrl);
                    }
                    break;
                case "Disable":
                    {
                        string userId = e.CommandArgument.ToString();
                        if (UserDisable(userId)) return;
                        Response.Redirect(HttpContext.Current.Request.RawUrl);
                    }
                    break;
                case "Enable":
                    {
                        string userId = e.CommandArgument.ToString();
                        if (UserEnable(userId)) return;
                        Response.Redirect(HttpContext.Current.Request.RawUrl);
                    }
                    break;
            }
        }

        /// <summary>
        /// 重置密码
        /// Resets the password.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        private bool ResetPassword(string userId)
        {
            T_USER_Entity userEntity = bll.GetEntity("USERID", userId);
            if (userEntity == null)
            {
                return true;
            }
            try
            {
                userEntity.PASSWORD =
                    UserLoginInfo.PasswordEncryption(configBll.GetCacheValueByName(this.Page,
                                                                                   "DefaultPassword"));
                bll.Update(userEntity);
                PageHelper.ShowMessage(this.Page, "密码重置成功！");
            }
            catch
            {
                PageHelper.ShowMessage(this.Page, "密码重置失败！");
            }
            return false;
        }

        /// <summary>
        /// 重置密码
        /// Resets the password.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        private bool UserDisable(string userId)
        {
            T_USER_Entity userEntity = bll.GetEntity("USERID", userId);
            if (userEntity == null)
            {
                return true;
            }
            try
            {
                userEntity.STATUS = (int)Status.禁用;
                    
                bll.Update(userEntity);
                PageHelper.ShowMessage(this.Page, "用户禁用成功！");
            }
            catch
            {
                PageHelper.ShowMessage(this.Page, "用户禁用失败！");
            }
            return false;
        }

        /// <summary>
        /// 重置密码
        /// Resets the password.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        private bool UserEnable(string userId)
        {
            T_USER_Entity userEntity = bll.GetEntity("USERID", userId);
            if (userEntity == null)
            {
                return true;
            }
            try
            {
                userEntity.STATUS = (int)Status.启用;
                bll.Update(userEntity);
                PageHelper.ShowMessage(this.Page, "用户启用成功！");
            }
            catch
            {
                PageHelper.ShowMessage(this.Page, "用户启用失败！");
            }
            return false;
        }

        

        #endregion



    }
}
